<?php
include_once("config.php");
include_once("functions.php");
session_start();
if (isset($_COOKIE['user']))
{
	$user = getUserByID($_COOKIE['user']);
	getInfo($user);
	header("location:draftpage.php");
}
$userName=$_POST['userName']; 
$password=$_POST['password'];
$user = checkLogin($userName, $password);
if ($user)
{
	getInfo($user);
	header("location:setcookie.php");
}
else
{
	echo "LOGIN FAILED";
	//header("location:login.php");
}

function getInfo($user)
{
	$_SESSION["user"] = $user;
	$teams = findTeams($user->id);
	if ($teams)
	{
		if ($teams.length > 1)
		{
			echo "TODO: account for someone having multiple teams";	
		}
		$_SESSION["team"]->$teams[0];
	}
	else
	{
		header("location:chooseteam.php");
	}
}
?>